﻿/*-----------------------------------------------
// File: LoginDataUpdate.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;

public class LoginAsyncDataAndRes : Fsm.FsmStateMonoBase<Login.LoginState>
{
    private enum UpdateState
    {
        COPY,
        UPDATING,
        UPDATE_OVER,
        INITING,

    }

    Login.LoginManager manager
    {
        get { return this.controller as Login.LoginManager; }
    }
    public LoginAsyncDataAndRes(Login.LoginState state, Login.LoginManager ctrl) : base(state, ctrl) { }
    public AssetUpdateManager updateManager;
    public LoginUI loginUI;
    private UpdateState state = UpdateState.UPDATING;

    public override void Enter()
    {
        updateManager = AssetUpdateManager.Instance;
		updateManager.Reset ();
        loginUI = manager.loginUI;
        state = UpdateState.UPDATING;
        //state = UpdateState.UPDATE_OVER;
        //return;

#if UNITY_EDITOR && !TESTASSET
        state = UpdateState.UPDATE_OVER;
#else

        // 更新完毕，初始化字典数据
        switch (GameConfig.Instance.programMethod)
        {
            case 0: //不需要强制程序
                break;
            case 1: //强制更新
                BoxManager.CreateMessageBoxWithCofirmAndCancelButton(LanguageTool.GetTextNoUpdate(1034),
                    ForceUpdate);
                return;
            case 2: //有程序可以更新
                BoxManager.CreateMessageBoxWithCofirmAndCancelButton(LanguageTool.GetTextNoUpdate(1035),
                    UserConfirmUpdate);
                return;
        }
        CheckResUpdate();
#endif
    }

    public void ForceUpdate(bool confirm)
    {
        if (confirm)
        {
            Application.OpenURL(GameConfig.Instance.appUrl);
            Application.Quit();
        }
        else
        {
            Application.Quit();
        }
    }

    public void UserConfirmUpdate(bool needUpdate)
    {
        if (needUpdate)
        {
            Application.OpenURL(GameConfig.Instance.appUrl);
            Application.Quit();
        }
        else
        {
            CheckResUpdate();
        }
    }

    public void CheckResUpdate()
    {
        switch (GameConfig.Instance.resMethod)
        {
            case ResourceUpdateMethod.NO_UPDATE:
                if (updateManager.NeedCopyData())
                {
                    loginUI.ShowUpdateUI();
                    loginUI.updatePanel.SetUpdateTipWord(LanguageTool.GetTextNoUpdate(1049));
                    updateManager.PrepareTableData(null);
                    state = UpdateState.COPY;
                }
                else
                {
                    state = UpdateState.UPDATE_OVER;
                }
                break;
            case ResourceUpdateMethod.FORCE_UPDATE:             // 资源更新
                BoxManager.CreateOneDelMsgBox(LanguageTool.GetTextNoUpdate(1033),
                    UpdateResource);
                break;
            case ResourceUpdateMethod.SLIENCE_UPDATE:             // 静默更新
                updateManager.BeginUpdate(GameConfig.Instance.resMethod);
                loginUI.ShowUpdateUI();
                break;
        }
    }

    public void UpdateResource()
    {
        updateManager.BeginUpdate(GameConfig.Instance.resMethod);
        loginUI.ShowUpdateUI();

    }

    public override void Leave()
    {
        loginUI.HideUpdateUI();
    }


    public override void Update()
    {
        switch (state)
        {
            case UpdateState.COPY:
                if (updateManager.State == AssetUpdateManager.AssetUpdateState.COMPLETE)
                {
                    state = UpdateState.UPDATE_OVER;
                }
                break;
            case UpdateState.UPDATING:

                break;
            case UpdateState.UPDATE_OVER:
                AssetLoad.AssetBundleManager.Instance.Initialize();
                state = UpdateState.INITING;
                return;
            case UpdateState.INITING:
                if (AssetLoad.AssetBundleManager.Instance.IsInitialized)
                {
                    StaticData.InitTableData();
                    controller.SwitchState(Login.LoginState.Notice);
                }
                return;
        }
        if (!GameConfig.Instance.IsDownloadOver)
        {
            if (updateManager.State >= AssetUpdateManager.AssetUpdateState.BIG_CHECKING && updateManager.State < AssetUpdateManager.AssetUpdateState.ERROR)
            {
                state = UpdateState.UPDATE_OVER;
                return;
            }
        }
        switch (updateManager.State)
        {
            case AssetUpdateManager.AssetUpdateState.NONE:
                break;
            case AssetUpdateManager.AssetUpdateState.COPY_DATA:                     // 10%
                loginUI.updatePanel.SetUpdateTipWord(LanguageTool.GetTextNoUpdate(1040));
                loginUI.updatePanel.SetUpdateSliderValue(0.1f);
                
                break;
            case AssetUpdateManager.AssetUpdateState.SMALL_CHECKING:                // 10% - 15%
                loginUI.updatePanel.SetUpdateSliderValue((float)((double)updateManager.loadedCount / updateManager.totalDownloadCount) * 0.05f + 0.1f);
                loginUI.updatePanel.SetUpdateTipWord(LanguageTool.GetTextNoUpdate(1040));
                break;
            case AssetUpdateManager.AssetUpdateState.SMALL_PACKAGE_DOWNLOAD:        // 15% - 45%
                loginUI.updatePanel.SetUpdateTipWord(LanguageTool.GetTextNoUpdate(1039) + "(" + GetMSize(updateManager.loadedSize) + "M/" + GetMSize(updateManager.totalSize) + "M" + ")");
                loginUI.updatePanel.SetUpdateSliderValue((float)((double)updateManager.loadedCount / updateManager.totalDownloadCount) * 0.3f + 0.15f);
                break;
            case AssetUpdateManager.AssetUpdateState.SMALL_PACKAGE_MOVE:            // 45%-50%
                loginUI.updatePanel.SetUpdateTipWord(LanguageTool.GetTextNoUpdate(1041));
                loginUI.updatePanel.SetUpdateSliderValue(0.5f);
                break;
            case AssetUpdateManager.AssetUpdateState.BIG_CHECKING:                  // 50%
                loginUI.updatePanel.SetUpdateTipWord(LanguageTool.GetTextNoUpdate(1040));
                loginUI.updatePanel.SetUpdateSliderValue(0.5f);
                break;
            case AssetUpdateManager.AssetUpdateState.BIG_DOWNLOAD:                  // 50-90%
                loginUI.updatePanel.SetUpdateTipWord(LanguageTool.GetTextNoUpdate(1039)
                    + "(" + GetMSize(updateManager.loadedSize) + "M/" + GetMSize(updateManager.totalSize) + "M" + ")");
                loginUI.updatePanel.SetUpdateSliderValue((float)((double)updateManager.loadedCount / updateManager.totalDownloadCount) * 0.4f + 0.5f);
                break;
            case AssetUpdateManager.AssetUpdateState.WAIT_FOR_MOVE:                 //90%
                loginUI.updatePanel.SetUpdateSliderValue(0.9f);
                break;
            case AssetUpdateManager.AssetUpdateState.BIG_MOVE:                      //90-95%
                loginUI.updatePanel.SetUpdateTipWord(LanguageTool.GetTextNoUpdate(1041));
                loginUI.updatePanel.SetUpdateSliderValue(0.95f);
                break;
            case AssetUpdateManager.AssetUpdateState.COMPLETE:                      // 100%
                loginUI.updatePanel.SetUpdateTipWord(LanguageTool.GetTextNoUpdate(1042));
                loginUI.updatePanel.SetUpdateSliderValue(1f);
                state = UpdateState.UPDATE_OVER;
                break;
            case AssetUpdateManager.AssetUpdateState.ERROR:
                if (!showError)
                {
                    showError = true;
                    BoxManager.CreateOneDelMsgBox(LanguageTool.GetTextNoUpdate(1043), ShowErrorCallback);
                }
                break;
        }
    }
    private bool showError = false;
    public void ShowErrorCallback()
    {
        showError = false;
        updateManager.BeginUpdate(GameConfig.Instance.resMethod);
    }

    public string GetMSize(long size)
    {
        return (((double)size) / (1 << 20)).ToString("F2");
    }
}